home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0007_3D Rotations.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  86 lines

  1. {
  2. STEVE CONNET
  3.  
  4. Okay, here's the equations For 3D rotations...
  5.  
  6. x,y,z are the coordinates of the point you want to rotate.
  7. rx,ry,rz are the amount of rotation you want (in degrees) For x,y,z
  8. }
  9.  
  10.   x1 := round(cos(rad(ry)) * x  - sin(rad(ry)) * z);
  11.   z1 := round(sin(rad(ry)) * x  + cos(rad(ry)) * z);
  12.   x  := round(cos(rad(rz)) * x1 + sin(rad(rz)) * y);
  13.   y1 := round(cos(rad(rz)) * y  - sin(rad(rz)) * x1);
  14.   z  := round(cos(rad(rx)) * z1 - sin(rad(rx)) * y1);
  15.   y  := round(sin(rad(rx)) * z1 + cos(rad(rx)) * y1);
  16.  
  17. {
  18. Because in Turbo Pascal, COS and SIN require radians For the argument,
  19. I wrote a short Function called RAD() that converts degrees into radians
  20. (I find degrees much easier to visualize)
  21. }
  22.  
  23.   Function Rad(i : Integer) : Real;
  24.   begin
  25.     Rad := i * (Pi / 360);
  26.   end;
  27.  
  28. {
  29. Of course, since most computers don't have 3D projection screens <G>,
  30. use these equations to provide a sense of perspective to the Object,
  31. but With 2D coordinates you can plot on a screen.
  32.  
  33. x,y,z are from the equations above, and xc,yc,zc are the center points
  34. for the Object that you are rotating... I recommend setting xc,yc at 0,0
  35. but zc should be very high (+100).
  36. }
  37.   x2 := trunc((xc * z - x * zc) / (z - zc));
  38.   y2 := trunc((yc * z - y * zc) / (z - zc));
  39.  
  40. {
  41. Alternatively, if you don't want to bother With perspective, just drop
  42. the z values, and just plot the (x,y) instead.
  43.  
  44.  
  45. To use these equations, pick a 3D Object and figure out what the 3D
  46. coordinates are For each point on the Object.  You will have to have some
  47. way to let the computer know which two points are connected.  For the
  48. cube that I did, I had one Array For the points and one For each face
  49. of the cube.  That way the computer can draw connecting lines For each
  50. face With a simple for-loop.
  51. }
  52.  
  53. Type
  54.   FaceLoc  = Array [1..4] of Integer;
  55.   PointLoc = Record
  56.     x, y, z : Integer;
  57.   end;
  58.  
  59. Const
  60.   face_c : Array [1..6] of faceloc =(
  61.     (1,2,3,4),
  62.     (5,6,2,1),
  63.     (6,5,8,7),
  64.     (4,3,7,8),
  65.     (2,6,7,3),
  66.     (5,1,4,8));
  67.  
  68.   point_c : Array [1..8] of pointloc =(
  69.     (-25, 25, 25),
  70.     ( 25, 25, 25),
  71.     ( 25,-25, 25),
  72.     (-25,-25, 25),
  73.     (-25, 25,-25),
  74.     ( 25, 25,-25),
  75.     ( 25,-25,-25),
  76.     (-25,-25,-25));
  77. {
  78. There you go.  I'm not going to get much more complicated For now.  if you
  79. can actually get these equations/numbers to work (and I haven't forgotten
  80. anything!) leave me another message, and I'll give you some advice for
  81. filling in the sides of the Object (so that you can only see 3 sides at
  82. once) and some advice to speed things up abit.  if you have any problems
  83. with whats here, show some other people, and maybe as a collective you can
  84. figure it out.  Thats how I got this one started!
  85. }
  86.